package com.andy.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.andy.utils.FilesUtil;

import java.io.File;

public class DBHelper extends SQLiteOpenHelper
{
    private static String TAG = "DBHelper";
    public static String databaseName = "coin_dog.db";

    //版数据库版本
    private static final int DATABASE_VERSION =2;
    private static DBHelper mdbHelper;
//    private Context mContext;

    public DBHelper(Context context) {
        super(context, FilesUtil.getAppDbPath() + File.separator + databaseName, null, DATABASE_VERSION);
//        super(new DatabaseContext(context), databaseName, null, DATABASE_VERSION);
//        mContext=context;
    }

    public DBHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, dbName, factory, version);
    }

    /**
     * 获取DBHelper实例（该方法仅供DBManager类使用，所有和数据库有关的参照有由DBManager统一管理）
     *
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        if (mdbHelper == null) {
//            DatabaseContext dbContext = new DatabaseContext(context);
//            mdbHelper = new DBHelper(dbContext);
            mdbHelper = new DBHelper(context);
        }
        return mdbHelper;
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e( TAG,"onCreate " );
        db.beginTransaction();
        try {
            db.execSQL(CoinInfo.CREATE_TABLE_Height);//创建一张表
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }


    /***
     * 数据库的升级操作，现在还没实现，后面根据表格的改变而修改
     * **/
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e( TAG,"onUpgrade " + databaseName + "从" + oldVersion + "升级到" + newVersion);
        db.beginTransaction();
        try {
            if(oldVersion < DATABASE_VERSION){
                //只要数据库版本号不一样,先删除表,然后再创建一个
                db.execSQL(CoinInfo.DROP_TABLE_Height);
                db.execSQL(CoinInfo.CREATE_TABLE_Height);

            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}
